using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using QuoteTree;


public partial class AppendNodes : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{

		if (Session.IsNewSession)
		{
			// If it says it is a new session, but an existing cookie exists, then it must 
			// have timed out (can't use the cookie collection because even on first 
			// request it already contains the cookie (request and response
			// seem to share the collection)
			string szCookieHeader = Request.Headers["Cookie"];
			if ((null != szCookieHeader) && (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
			{
				Response.Redirect("sessionTimeout.aspx");
			}
		}
		if (this.Session["tree"] != null && this.Request.QueryString["node"] != null && this.Request.QueryString["node"].ToString() != "")
		{
			QTree t = this.Session["tree"] as QTree;
			string node = this.Request.QueryString["node"].ToString().Trim();
			this.IFRAME2.Attributes.Add("src", "quotes/" + node.Replace("\\", "/") + "/homepage.htm");
			SumSetNode n = t.GetNodeFromPath(node) as SumSetNode;
			if (n.hidden) this.Page.Controls.Clear();
			//decimal res;
			if (!IsPostBack)
			{
				foreach (INode child in n.children)
				{
					if (child.template)
					{
						ListItem item = new ListItem(child.name, node + "\\" + child.name);
						this.CheckBoxList1.Items.Add(item);
					}
				}
			}


		}
		else this.Page.Controls.Clear();
			
	}
	protected void LinkButton1_Click(object sender, EventArgs e)
	{
		string node = this.Request.QueryString["node"].ToString().Trim();
		QTree t = this.Session["tree"] as QTree;
		INode n = t.GetNodeFromPath(node);


		foreach (ListItem item in this.CheckBoxList1.Items)
		{
			if (item.Selected)
			{
				INode child;
				INode clone = t.GetNodeFromPath(item.Value);
				if (clone != null)
				{
					child = clone.Clone();
					child.template = false;
					child.hidden = false;
					child.name = child.name + n.children.Count;
					n.children.Add(child);
					n.expanded = true;
				}
			}
		}
		this.Session["leftframe"] = this.Request.RawUrl;
		this.Response.Write("<script language='javascript'> {parent.location.href=\"javascript:__doPostBack('Button2','')\"}</script>");

	}
}
